package edu.gmu.projects.fireresponder.environment;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import edu.gmu.projects.util.geo.CoordinateCalculator;

public class Path extends Asset {
	final int TIME_TO_COMPLETE = 300;
	private Location p1;
	private Location p2;
	private Location center;
	private double distance;
	private Set<Path> adjacentPaths;
	private Set<Building> adjacentBuildings;
	private Set<Safespot> adjacentSafeSpots;
	private Set<Asset> connectedAssets;
	
	public Path() {
		adjacentPaths = new HashSet<Path>();
		adjacentBuildings = new HashSet<Building>();
		adjacentSafeSpots = new HashSet<Safespot>();
		connectedAssets = new HashSet<Asset>();
	}
	
	public Location getP1() {
		return p1;
	}

	public void setP1(Location p1) {
		this.p1 = p1;
		updateDistance();
	}

	public Location getP2() {
		return p2;
	}

	public void setP2(Location p2) {
		this.p2 = p2;
		updateDistance();
	}
	
	private void updateDistance() {
		if(p1 != null && p2 != null) {
			distance = CoordinateCalculator.getDistance(p1, p2);

			center = new Location();
			center.setLatitude((p1.getLatitude() + p2.getLatitude()) / 2);
			center.setLongitude((p1.getLongitude() + p2.getLongitude()) / 2);
		}
	}
	
	public double getDistance() {
		return distance;
	}

	public boolean isConnectedToPath(Path path) {
		return adjacentPaths.contains(path);

		/*
		return getP1().equals(path.getP1()) ||
		getP1().equals(path.getP2()) ||
		getP2().equals(path.getP1()) ||
		getP2().equals(path.getP2());
		*/
	}

	public void addAdjacentPath(Path path) {
		adjacentPaths.add(path);
		connectedAssets.add(path);
	}
	
	public Collection<Path> getAdjacentPaths() {
		return adjacentPaths;
	}

	public boolean isConnectedToBuilding(Building building) {
		return adjacentBuildings.contains(building);
	}

	public void addAdjacentBuilding(Building building) {
		adjacentBuildings.add(building);
		connectedAssets.add(building);
	}
	
	public Collection<Building> getAdjacentBuildings() {
		return adjacentBuildings;
	}

	public boolean isConnectedToSafeSpot() {
		return ! adjacentSafeSpots.isEmpty();
	}

	public boolean isConnectedToSafespot(Safespot safeSpot) {
		return adjacentSafeSpots.contains(safeSpot);
	}

	public void addAdjacentSafeSpot(Safespot safeSpot) {
		adjacentSafeSpots.add(safeSpot);
		connectedAssets.add(safeSpot);
	}
	
	public Collection<Safespot> getAdjacentSafeSpots() {
		return adjacentSafeSpots;
	}


	public int getEstimatedTimeToComplete() {
		return getEstimatedTimeToComplete(Route.DEFAULT_SPEED);
	}

	public int getEstimatedTimeToComplete(double kmperhour) {
		return (int) (distance * 3600 / kmperhour);
	}

	public int compareTo(Asset o) {
		return 0;
	}

	public Set<Location> getPoints() {
		Set<Location> points = new HashSet<Location>();
		points.add(p1);
		points.add(p2);
		return points;
	}

	public Location getCenter() {
		return center;
	}

	public Collection<Asset> getConnectedAssets() {
		return connectedAssets;
	}
	
	public String toString() {
		return "Path: " + getId() + " (" + getP1() + " to " + getP2() + ")";
	}
}